package org.edu.jgame.cardgames;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;


public class MainMenuState extends BasicGameState {
	
	int stateID = -1;
	
	// Cac hinh can dung trung Menu Option
	Image background = null;
	Image[] play = null;
	Image[] option = null;
	Image[] credit = null;
	Image[] exit = null;
	
	// Chi muc cho cac hinh
	int idx_play = 0;
	int idx_option = 0;
	int idx_credit = 0;
	int idx_exit = 0;
	
	// Toa do cua cac hinh
	float playX = 0.0f;
	float playY = 0.0f;
	
	float optionX = 0.0f;
	float optionY = 0.0f;
	
	float creditX = 0.0f;
	float creditY = 0.0f;
	
	float exitX = 0.0f;
	float exitY = 0.0f;
	
	Sound sclick = null;
	Sound fx = null;
	boolean fx_played = false;
	
	public MainMenuState(int stateID) {
		this.stateID = stateID;	
	}

	// Load cac hinh vao cac sprite de dung
	@Override
	public void init(GameContainer gameContainer, StateBasedGame sbg)
			throws SlickException {		
		background = new Image("data/Menu Image.png");
		
		play = new Image[2];
		play[0] = new Image("data/PlayGame_Button.png");
		play[1] = new Image("data/PlayGame_Button_Select.png");
		playX = 44.0f;
		playY = 110.0f;
		
		option = new Image[2];
		option[0] = new Image("data/Option_Button.png");
		option[1] = new Image("data/Option_Button_Select.png");
		optionX = 88.0f;
		optionY = 187.0f;
		
		
		credit = new Image[2];
		credit[0] = new Image("data/Credit_Button.png");
		credit[1] = new Image("data/Credit_Button_Select.png");
		creditX = 73.0f;
		creditY = 270.0f;
		
		exit = new Image[2];
		exit[0] = new Image("data/Exit_Button.png");
		exit[1] = new Image("data/Exit_Button_Select.png");
		exitX = 53.0f;
		exitY = 361.0f;
		
		fx = new Sound("data/tick.wav");
		sclick = new Sound("data/click.wav");
	}

	// Ve cac sprite len man hinh game
	@Override
	public void render(GameContainer gameContainer, StateBasedGame sbg, Graphics graphics)
			throws SlickException {		
		background.draw(0,0);
		
		play[idx_play].draw(playX, playY);
		option[idx_option].draw(optionX, optionY);
		credit[idx_credit].draw(creditX, creditY);
		exit[idx_exit].draw(exitX, exitY);
	}

	// Xu ly control va logic cho cac sprite
	@Override
	public void update(GameContainer gameContainer, StateBasedGame sbg, int delta)
			throws SlickException {		
		Input input = gameContainer.getInput();
		
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		
		boolean insideplay = false;
		boolean insideoption = false;
		boolean insidecredit = false;
		boolean insideexit = false;
		
		// Kiem tra toa do cua chuot so voi cac sprite 
		if((mouseX >= playX && mouseX <= playX + play[idx_play].getWidth()) 
				&& (mouseY >= playY && mouseY <= playY + play[idx_play].getHeight())) {
			insideplay = true;
		}
		else if((mouseX >= optionX && mouseX <= optionX + option[idx_option].getWidth()) 
				&& (mouseY >= optionY && mouseY <= optionY + option[idx_option].getHeight())) {
			insideoption = true;
		}
		else if((mouseX >= creditX && mouseX <= creditX + credit[idx_credit].getWidth()) 
				&& (mouseY >= creditY && mouseY <= creditY + credit[idx_credit].getHeight())) {
			insidecredit = true;
		}
		else if((mouseX >= exitX && mouseX <= exitX + exit[idx_exit].getWidth()) 
				&& (mouseY >= exitY && mouseY <= exitY + exit[idx_exit].getHeight())) {
			insideexit = true;
		}
		else {
			fx_played = false;
		}
		
		if(insideplay) {
			idx_play = 1;
			
			playX = 43.0f;
			playY = 109.0f;
			
			if(!fx_played) {
				fx.play();
				fx_played = true;
			}
			
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {				
				sclick.play();
				sbg.enterState(JCardGame.CONNECTSTATE);
			}
		}
		else {
			idx_play = 0;
			
			playX = 44.0f;
			playY = 110.0f;
		}
		
		if(insideoption) {
			idx_option = 1;
			
			optionX = 87.0f;
			optionY = 186.0f;
			
			if(!fx_played) {
				fx.play();
				fx_played = true;
			}
			
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {				
				sclick.play();
				sbg.enterState(JCardGame.OPTIONSTATE);
			}
		}
		else {
			idx_option = 0;
			
			optionX = 88.0f;
			optionY = 187.0f;
		}
		
		if(insidecredit) {
			idx_credit = 1;
			
			creditX = 72.0f;
			creditY = 269.0f;
			
			if(!fx_played) {
				fx.play();
				fx_played = true;
			}
			
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {				
				sclick.play();
				sbg.enterState(JCardGame.CREDITSTATE);
			}
		}
		else {
			idx_credit = 0;
			
			creditX = 73.0f;
			creditY = 270.0f;
		}
		
		if(insideexit) {
			idx_exit = 1;
			
			exitX = 52.0f;
			exitY = 360.0f;
			
			if(!fx_played) {
				fx.play();
				fx_played = true;
			}
			
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				sclick.play();
				gameContainer.exit();
			}
		}
		else {
			idx_exit = 0;
			
			exitX = 53.0f;
			exitY = 361.0f;
		}
	}

	@Override
	public int getID() {		
		return stateID;
	}

}
